const ajax = {
    get(url,data){
        return new Promise((resolve, reject)=>{
            myAjax(url,data,"get",resolve,reject);
        })
    },
    post(url,data){
        return new Promise((resolve, reject)=>{
            myAjax(url,data,"post",resolve,reject);
        })
    },
    delete(url,data){
        return new Promise((resolve, reject)=>{
            myAjax(url,data,"delete",resolve,reject);
        })
    },
}

function myAjax(url,data,method,successFun,errorFun) {
    $.ajax({
        url,
        method,
        data,
        success(res){
            if(res.code === 200){
                successFun(res.data);
            }else if(res.code === 401){
                layer.alert(res.msg,{icon:5},function (index) {
                    top.location.replace("/");
                    layer.close(index);
                })
            }else{
                layer.alert(res.msg,{icon:5})
            }
        },
        error(e){
            console.log(e);
            layer.alert("与服务器断开连接...");
            errorFun(e);
        }
    })
}


const formatDate=(date)=>{
    const year = date.getFullYear();
    const month= date.getMonth()+1;
    const d = date.getDate();
    return `${year}-${month<10?'0'+month:month}-${d<10?'0'+d:d}`
}

const formatDateTime=(date)=>{
    const year = date.getFullYear();
    const month= date.getMonth()+1;
    const d = date.getDate();
    const h = date.getHours();
    const m = date.getMinutes();
    const s = date.getSeconds();
    return `${year}-${month<10?'0'+month:month}-${d<10?'0'+d:d} ${h<10?'0'+h:h}:${m<10?'0'+m:m}:${s<10?'0'+s:s}`
}
